﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO.Ports;
using System.Threading;
namespace SerialPortWrite
{
    class Program
    {
        static void Main(string[] args)
        {
            using (SerialPort s = new SerialPort("COM10"))
            {
                s.Open();

                s.ErrorReceived += new SerialErrorReceivedEventHandler(s_ErrorReceived);

                Console.WriteLine("Baud Rate" + s.BaudRate);
                Console.WriteLine("Break State" + s.BreakState);
                Console.WriteLine("CD Holding" + s.CDHolding);
                Console.WriteLine("CTS Holding" + s.CtsHolding);
                Console.WriteLine("DataBits" + s.DataBits);
                Console.WriteLine("DrsHolding" + s.DsrHolding);
                Console.WriteLine("DrsEnable" + s.DtrEnable);
                Console.WriteLine("Encoding" + s.Encoding);
                Console.WriteLine("Handshake" + s.Handshake);
                Console.WriteLine("Parity" + s.Parity);
                Console.WriteLine("RtsEnable" + s.RtsEnable);
                Console.WriteLine("StopBits" + s.StopBits);
                Console.WriteLine("WriteBufferSize" + s.WriteBufferSize);
                Console.WriteLine("Write Timeout" + s.WriteTimeout);

                while (s.IsOpen)
                {
                    //byte[] b = { 1 };
                    //s.Write(b, 0, 1);
                    //Thread.Sleep(100);

                    for (byte i = 0; i <= 255; i++)
                    {

                        byte[] b = { i };
                        s.Write(b, 0, 1);
                        Thread.Sleep(1000);
                    }
                }
            }
            //s.BaudRate = 9600;
            //s.DataBits = 8;
            //s.Parity = Parity.None;
        }

        static void s_ErrorReceived(object sender, SerialErrorReceivedEventArgs e)
        {
            Console.WriteLine(e.EventType);
        }
    }
}
